﻿Imports System
Imports System.Collections.Generic
Imports System.Drawing

Namespace System.Workflow.ComponentModel.Design
    Public Class ConnectionPoint
        ' Methods
        Public Sub New(ByVal associatedDesigner As ActivityDesigner, ByVal designerEdge As DesignerEdges, ByVal connectionIndex As Integer)
            If (associatedDesigner Is Nothing) Then
                Throw New ArgumentNullException("associatedDesigner")
            End If
            If ((connectionIndex < 0) OrElse (connectionIndex >= associatedDesigner.GetConnections(designerEdge).Count)) Then
                Throw New ArgumentException(DR.GetString("Error_ConnectionPoint", New Object(0  - 1) {}), "connectionIndex")
            End If
            Me.associatedDesigner = associatedDesigner
            Me.designerEdge = designerEdge
            Me.connectionIndex = connectionIndex
        End Sub

        Friend Shared Sub Draw(ByVal e As ActivityDesignerPaintEventArgs, ByVal bounds As Rectangle)
            bounds.Inflate(-1, -1)
            e.Graphics.FillEllipse(Brushes.White, bounds)
            e.Graphics.DrawEllipse(e.AmbientTheme.SelectionForegroundPen, bounds)
            bounds.Inflate(-1, -1)
            e.Graphics.FillEllipse(e.AmbientTheme.SelectionForegroundBrush, bounds)
        End Sub

        Public Overrides Function Equals(ByVal obj As Object) As Boolean
            Dim point As ConnectionPoint = [TryCast](obj,ConnectionPoint)
            If (point Is Nothing) Then
                Return False
            End If
            Return (((point.AssociatedDesigner Is Me.associatedDesigner) AndAlso (point.designerEdge = Me.designerEdge)) AndAlso (point.ConnectionIndex = Me.connectionIndex))
        End Function

        Public Overrides Function GetHashCode() As Integer
            Return ((Me.associatedDesigner.GetHashCode Xor Me.designerEdge.GetHashCode) Xor Me.connectionIndex.GetHashCode)
        End Function

        Public Sub OnPaint(ByVal e As ActivityDesignerPaintEventArgs, ByVal drawHighlighted As Boolean)
            ConnectionPoint.Draw(e, Me.Bounds)
        End Sub


        ' Properties
        Public ReadOnly Property AssociatedDesigner As ActivityDesigner
            Get
                Return Me.associatedDesigner
            End Get
        End Property

        Public Overridable ReadOnly Property Bounds As Rectangle
            Get
                If (Me.associatedDesigner.GetConnections(DesignerEdges.All).Count > 0) Then
                    Dim location As Point = Me.Location
                    Dim defaultSize As Size = Me.DefaultSize
                    Return New Rectangle(New Point((location.X - (defaultSize.Width / 2)), (location.Y - (defaultSize.Height / 2))), defaultSize)
                End If
                Return Rectangle.Empty
            End Get
        End Property

        Public ReadOnly Property ConnectionEdge As DesignerEdges
            Get
                Dim designerEdge As DesignerEdges = Me.designerEdge
                If (((designerEdge <> DesignerEdges.Left) AndAlso (designerEdge <> DesignerEdges.Right)) AndAlso ((designerEdge <> DesignerEdges.Top) AndAlso (designerEdge <> DesignerEdges.Bottom))) Then
                    designerEdge = DesignerGeometryHelper.ClosestEdgeToPoint(Me.Location, Me.associatedDesigner.Bounds, designerEdge)
                End If
                Return designerEdge
            End Get
        End Property

        Public ReadOnly Property ConnectionIndex As Integer
            Get
                Return Me.connectionIndex
            End Get
        End Property

        Private ReadOnly Property DefaultSize As Size
            Get
                Dim margin As Size = WorkflowTheme.CurrentTheme.AmbientTheme.Margin
                margin.Width = (margin.Width + (margin.Width / 2))
                margin.Height = (margin.Height + (margin.Height / 2))
                If (Not Me.associatedDesigner Is Nothing) Then
                    margin = New Size(Math.Max(margin.Width, (CInt(Me.associatedDesigner.DesignerTheme.ForegroundPen.Width) * 4)), Math.Max(margin.Height, (CInt(Me.associatedDesigner.DesignerTheme.ForegroundPen.Width) * 4)))
                End If
                Return margin
            End Get
        End Property

        Public Overridable ReadOnly Property Location As Point
            Get
                Dim connections As IList([Of] Point) = Me.associatedDesigner.GetConnections(Me.designerEdge)
                If (Me.connectionIndex < connections.Count) Then
                    Return connections.Item(Me.connectionIndex)
                End If
                Return Point.Empty
            End Get
        End Property


        ' Fields
        Private associatedDesigner As ActivityDesigner
        Private connectionIndex As Integer
        Private designerEdge As DesignerEdges
    End Class
End Namespace

